package cn.sz.hy.ui.frame;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.FormToolkit;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;

import cn.sz.hy.adapter.center.AddColumnAdapter;
import cn.sz.hy.adapter.center.DelColumnAdapter;
import cn.sz.hy.adapter.center.DownColumnAdapter;
import cn.sz.hy.adapter.center.UpColumnAdapter;
import cn.sz.hy.common.EventBus;
import cn.sz.hy.common.Globel;
import cn.sz.hy.logic.strategy.vo.SheetItem;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.layout.GridData;
import cn.sz.hy.ui.widget.center.Row;
import org.eclipse.swt.widgets.Combo;

public class CenterPanel extends Composite {

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Composite composite;
	public Composite rowList;
	private List<SheetItem> backItems;
	private List<String> backNameList;
	private List<Row> rows = new ArrayList<Row>();
	private static final int ROW_SIZE = 20;
	private final EventBus eBus = EventBus.getInstance();
	private Combo editList;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public CenterPanel(Composite parent, int style) {
		super(parent, style);
		setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				toolkit.dispose();
			}
		});
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		setLayout(new BorderLayout(0, 0));

		composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager
				.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
		composite.setLayoutData(BorderLayout.EAST);
		toolkit.adapt(composite);
		toolkit.paintBordersFor(composite);
		composite.setLayout(new GridLayout(1, false));

		Button button_1 = new Button(composite, SWT.NONE);
		GridData gd_button_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_1.heightHint = 30;
		gd_button_1.widthHint = 89;
		button_1.setLayoutData(gd_button_1);
		button_1.addMouseListener(new AddColumnAdapter());
		toolkit.adapt(button_1, true, true);
		button_1.setText("添加字段");

		Button button = new Button(composite, SWT.NONE);
		GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button.heightHint = 30;
		gd_button.widthHint = 88;
		button.setLayoutData(gd_button);
		toolkit.adapt(button, true, true);
		button.setText("删除字段");
		button.addMouseListener(new DelColumnAdapter(this));
		new Label(composite, SWT.NONE);

		Button button_2 = new Button(composite, SWT.NONE);
		GridData gd_button_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_2.heightHint = 30;
		gd_button_2.widthHint = 89;
		button_2.setLayoutData(gd_button_2);
		button_2.addMouseListener(new UpColumnAdapter(this));
		toolkit.adapt(button_2, true, true);
		button_2.setText("字段上移");

		Button button_3 = new Button(composite, SWT.NONE);
		GridData gd_button_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_button_3.heightHint = 30;
		gd_button_3.widthHint = 89;
		button_3.setLayoutData(gd_button_3);
		button_3.addMouseListener(new DownColumnAdapter(this));
		toolkit.adapt(button_3, true, true);
		button_3.setText("字段下移");
		new Label(composite, SWT.NONE);

		Label label = new Label(composite, SWT.NONE);
		toolkit.adapt(label, true, true);
		label.setText("选择编辑字段:");

		editList = new Combo(composite, SWT.NONE);
		editList.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		toolkit.adapt(editList);
		toolkit.paintBordersFor(editList);

		rowList = new Composite(this, SWT.H_SCROLL | SWT.V_SCROLL);
		rowList.setRedraw(true);
		rowList.setLayoutData(BorderLayout.CENTER);
		toolkit.adapt(rowList);
		toolkit.paintBordersFor(rowList);
		rowList.setLayout(new GridLayout(1, true));

		for (int i = 0; i < ROW_SIZE; i++) {
			Row row = new Row(rowList, SWT.NONE);
			row.setVisible(false);
			toolkit.adapt(row);
			toolkit.paintBordersFor(row);
			rows.add(row);
		}

		eBus.addComponent(Globel.CENTER_PANEL_ID, this);

	}

	private void addRow(SheetItem item, int index) {
		Row row = rows.get(index);
		row.getText().setText(item.getName());
		row.setItem(item);
		row.setVisible(true);
	}

	public void loadItem(List<SheetItem> mainList) {
		clearList();
		for (int i = 0; i < mainList.size(); i++) {
			SheetItem item = mainList.get(i);
			addRow(item, i);
		}
		updateEditSelect();
	}

	public void createBlankItem() {
		for (int i = 0; i < rows.size(); i++) {
			Row row = rows.get(i);
			if (!row.getVisible()) {
				if (i == 0) {
					break;
				}
				row.getText().setText("请输入字段名称");
				row.loadBackList(backNameList);
				row.setVisible(true);
				break;
			}
		}

	}

	public void uploadBackupItemList(List<SheetItem> items) {
		if (items == null || items.size() == 0) {
			return;
		}
		backItems = items;
		backNameList = new ArrayList<String>();
		for (SheetItem item : backItems) {
			backNameList.add(item.getName());
		}
		for (Row row : rows) {
			if (row.getVisible()) {
				row.loadBackList(backNameList);
			}
		}
	}

	public void clearList() {
		for (Row row : rows) {
			if (row.getVisible()) {
				row.restart();
			}
		}
	}

	public List<String> getRowNames() {
		List<String> rowName = new ArrayList<String>();
		for (Row row : rows) {
			if (row.getVisible()) {
				rowName.add(row.getText().getText());
			}
		}
		return rowName;
	}

	public void updateEditSelect() {
		editList.removeAll();
		for (Row row : rows) {
			if (row.getVisible()) {
				editList.add(row.getText().getText());
			}
		}
		editList.select(0);
	}

	public List<SheetItem> getBackItems() {
		return backItems;
	}

	public synchronized void removeRow(int index) {
		for (int i = index + 1; i < rows.size(); i++) {
			Row tmp = rows.get(i);
			if (!tmp.getVisible()) {
				Row row = rows.get(i - 1);
				row.restart();
				break;
			}
			changeRow(i, i - 1);
		}
	}

	public synchronized void changeRow(int beforeIndex, int arfterIndex) {
		if (arfterIndex < 0 || arfterIndex > rows.size() - 1) {
			return;
		}
		Row row_b = rows.get(beforeIndex);
		Row row_a = rows.get(arfterIndex);
		if (!row_a.getVisible()) {
			return;
		}
		String text_b = row_b.getText().getText();
		int type_b = row_b.getLink().getSelectionIndex();
		int selection_b= row_b.getBackup().getSelectionIndex();
		SheetItem item_b = row_b.getItem();
		
		row_b.getText().setText(row_a.getText().getText());
		int selection_a = row_a.getBackup().getSelectionIndex();
		row_b.getLink().select(row_a.getLink().getSelectionIndex());
		row_b.getBackup().select(selection_a);
		row_b.setItem(row_a.getItem());
		
		row_a.getText().setText(text_b);
		row_a.getLink().select(type_b);
		row_a.getBackup().select(selection_b);
		row_a.setItem(item_b);
		
		editList.select(arfterIndex);
	}

	public List<Row> getRows() {
		return rows;
	}

	public Combo getEditList() {
		return editList;
	}

}
